package com.example.personal.entity.vo;

import com.example.personal.entity.HttpStatusEnum;
import lombok.Data;

/**
 * Created on 2022/3/9.
 *
 * @author taodq
 */

@Data
public class CommonResult<T> {

    // 响应码
    private int code;

    // 响应消息
    private String msg;

    // 响应数据
    private T data;

    public CommonResult() {
    }

    /***
     * 成功响应
     * @param data 响应数据
     * @return CommonResult
     */
    public static <T> CommonResult ok(T data) {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(HttpStatusEnum.SUCCESS.getCode());
        commonResult.setMsg(HttpStatusEnum.SUCCESS.getMsg());
        commonResult.setData(data);
        return commonResult;
    }

    /***
     * 成功响应
     * @return CommonResult
     */
    public static CommonResult ok() {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(HttpStatusEnum.SUCCESS.getCode());
        commonResult.setMsg(HttpStatusEnum.SUCCESS.getMsg());
        return commonResult;
    }

    /***
     * 失败响应
     * @return CommonResult
     */
    public static CommonResult error() {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(HttpStatusEnum.FAIL.getCode());
        commonResult.setMsg(HttpStatusEnum.FAIL.getMsg());
        return commonResult;
    }

    /***
     * 失败响应
     * @param msg 失败消息
     * @return CommonResult
     */
    public static CommonResult error(String msg) {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(HttpStatusEnum.FAIL.getCode());
        commonResult.setMsg(msg);
        return commonResult;
    }
}
